package com.bt.lms.ui.action.registration.stu;

import com.alibaba.fastjson.JSON;


import com.bt.lms.constants.RegStuBizTypeConstant;
import com.bt.lms.entity.RegStuInfo;
import com.bt.lms.network.msg.ReqMsg;

import com.bt.lms.network.msg.RespMsg;
import com.bt.lms.ui.page.stu.PageStuDetailInfo;
import com.bt.lms.ui.page.common.action.pageenp.ApplyStuLabAction;
import com.bt.lms.ui.page.stu.vo.RegistrationStuVo;

import javafx.event.Event;
import javafx.event.EventHandler;
import javafx.scene.layout.Pane;
import org.apache.commons.beanutils.BeanUtils;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.lang.reflect.InvocationTargetException;
import java.net.Socket;

/**
 * 主界面张三标签详细信息事件
 */
public class PageMainZSLabAction implements EventHandler {

    @Override

    public void handle(Event event) {


        //报名学员信息vo
        RegistrationStuVo registrationStuVo = new RegistrationStuVo();

        //1.组包
        byte[] bytes = handleReqMsg(registrationStuVo);


        //2.组网
        Socket socket = getNet();

        //3.发送消息
        boolean flag = sendReq(socket, bytes);


        //张三标签事件
        PageStuDetailInfo pageStuDetailInfo = new PageStuDetailInfo();
        Pane pane = ApplyStuLabAction.pageSkipInfo.getPane();
        Integer index = ApplyStuLabAction.pageSkipInfo.getIndex();
        pane.getChildren().set(index, pageStuDetailInfo);

    }

    /**
     * 发送消息
     *
     * @param socket 网络
     * @param bytes  byte 数组
     * @return 是否成功
     */
    private boolean sendReq(Socket socket, byte[] bytes) {


        try {
            OutputStream out = socket.getOutputStream();
            out.write(bytes);
            out.flush();


            byte[]buffer = new byte[1024];
            InputStream in = socket.getInputStream();
            in.read(buffer);
            in.close();
            out.close();
            socket.close();


        } catch (IOException e) {
//            throw new RuntimeException(e);
        }


        return true;
    }


    /**
     * 组网
     *
     * @return 网络
     */
    private Socket getNet() {
        String serverIP = "127.0.0.1";
        int serverPort = 8888;
        Socket socket = null;
        try {
            socket = new Socket(serverIP, serverPort);
        } catch (IOException e) {
//            throw new RuntimeException(e);
            return null;
        }
        return socket;
    }


    /**
     * 组包
     *
     * @param registrationStuVo 报名学员信息
     * @return 消息
     */
    private byte[] handleReqMsg(RegistrationStuVo registrationStuVo) {


        //请求消息
        ReqMsg reqMsg = new ReqMsg();

        //报名学员信息
        RegStuInfo regStuInfo = new RegStuInfo();


        //业务类型
        reqMsg.setBizType(RegStuBizTypeConstant.STU_REG_INFO_QUERY_DETAIL);

        //请求id时间戳
        long l = System.currentTimeMillis();
        reqMsg.setReqId(l);


        try {

            //vo转entity
            BeanUtils.copyProperties(regStuInfo, registrationStuVo);


            String regStuStr = JSON.toJSONString(regStuInfo);

            //带走
            reqMsg.setData(regStuStr);


        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        } catch (InvocationTargetException e) {
            throw new RuntimeException(e);
        }
        //转成总数组
        byte[] bytes = reqMsg.toBytes();


        return bytes;
    }


}
